package api

import (
	"context"
	"fmt"
	"github.com/icetech233/gin"
	"io"
	"log/slog"
	"logengine/core"
	"time"
)

var (
	l   slog.Level      = slog.LevelWarn
	ctx context.Context = context.Background()
)

func init() {

}

func InitRouterV1(engine *gin.Engine) *gin.Engine {
	// mid
	engine.Use(gin.Logger(), gin.Recovery())
	//engine.Use(sentrygin.New(sentrygin.Options{}))
	engine.GET("/ping", ping)
	engine.POST("/v1/devlog", devlog)
	engine.POST("/v1/recmsg", RecMsgV1)
	engine.POST("/v2/recmsg", RecMsgV2)
	engine.POST("/v3/recpdf", RecPdfV3)
	engine.POST("/v3/reccsv", RecCsvV3)
	engine.NoRoute(noRoute)
	return engine
}
func RecCsvV3(c *gin.Context) {
	// SetHeader("symbol", symbol).
	symbol := c.Request.FormValue("symbol")

	file, header, err := c.Request.FormFile("csv_file")
	if err != nil {
		c.String(500, "Failed to retrieve file")
		return
	}
	var size int64 = header.Size
	var filename string = header.Filename
	fileBytes, _ := io.ReadAll(file)
	if len(fileBytes) != int(size) {
		fmt.Println("file size != is ", size)
	}

	slog.Warn("rec_"+filename, "symbol", symbol, "size", size, "file_len", len(fileBytes))

	// 备份文件
	_ = core.BackFile(filename, fileBytes)

	// TR20240718_160048.csv
	fileTime, _ := time.Parse("20060102", filename[2:len("TR20240718")])
	slog.Warn("file_time_"+filename, "file_time", fileTime)

	msgDatas, err := core.DataTransform(fileTime, fileBytes, symbol) // (model.BatchMessageData, error)
	if err != nil {
		c.String(500, "data transform err "+err.Error())
		return
	}
	//
	for _, msgData := range msgDatas {
		core.SaveMq(*msgData)
	}
	c.JSON(200, gin.H{"code": 0, "msg": "ok"})
}

func RecPdfV3(c *gin.Context) {
	file, header, err := c.Request.FormFile("pdf_file")
	if err != nil {
		c.String(500, "Failed to retrieve file")
		return
	}
	var size int64 = header.Size
	var filename string = header.Filename
	fileBytes, _ := io.ReadAll(file)
	//
	if len(fileBytes) == int(size) {
		fmt.Println("file size == is", size)
		fmt.Println("filename =", filename)
	} else {
		fmt.Println("file size != is ", size)
	}
	c.JSON(200, gin.H{"code": 0, "msg": "ok"})
}

func RecMsgV1(c *gin.Context) {
	jsondata, err := c.GetRawData()
	if err != nil {
		return
	}
	slog.Warn("remote:"+c.Request.RemoteAddr, "context", string(jsondata))
	//
	go core.Work(string(jsondata))
	c.JSON(200, gin.H{"code": 0, "msg": "ok"})
}

func RecMsgV2(c *gin.Context) {
	strarr := make([]string, 0, 40)
	c.BindJSON(&strarr)
	//
	slog.Warn("remote:"+c.Request.RemoteAddr, "rec len", len(strarr))
	go core.WorkArray(strarr)
	//
	c.JSON(200, gin.H{"code": 0, "msg": "ok"})
}

func noRoute(c *gin.Context) {
	slog.Error("noRoute error", "remote", c.Request.RemoteAddr, "uri", c.Request.RequestURI)
	c.JSON(200, gin.H{"code": -1, "msg": "no route"})
}

func devlog(c *gin.Context) {
	jsonData, err := c.GetRawData()
	if err != nil {
		return
	}
	//
	var l slog.Level = slog.LevelInfo
	_levelStr := c.Request.Header.Get("jx-log")
	if _levelStr == "Debug" {
		l = slog.LevelDebug
	} else if _levelStr == "Info" {
		l = slog.LevelInfo
	} else if _levelStr == "Warn" {
		l = slog.LevelWarn
	} else if _levelStr == "Error" {
		l = slog.LevelError
	}
	//
	slog.Log(ctx, l, "remote:"+c.Request.RemoteAddr, "context", string(jsonData))

	jsonData = nil
	c.JSON(200, gin.H{"code": 0, "msg": "ok"})
}
